package com.Policing;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;



import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.Policing.Fragment.Allot;
import com.Policing.Fragment.Fragment1;
import com.Policing.Fragment.Fragment2;
import com.Policing.Fragment.Fragment3;
import com.Policing.Fragment.Fragment4;
import com.Policing.Fragment.Fragment5;
import com.Policing.Fragment.Fragment6;
import com.Policing.Fragment.Fragment7;
import com.Policing.Fragment.Repairs;
import com.Policing.Fragment.Scrap;
import com.Policing.menu.Menu_Adapter;
import com.Policing.menu.Menu_bean;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity implements OnItemClickListener {

    public DrawerLayout drawerLayout;
    private ListView listView;
    private List<Menu_bean> linkMains = new ArrayList<>();
    private long mExitTime;
    private FragmentManager fragmentManager;
    private Fragment fragment1, fragment2, fragment3,fragment4,fragment5,fragment6,fragment7,Allot,Scrap,Repairs;
    private String[] str;
    private  String user="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        user = mm.getString("user","");
        XGPushManager.registerPush(this, user,
                new XGIOperateCallback() {
                    @Override
                    public void onSuccess(Object data, int flag) {
                        Log.d("TPush", "注册成功，设备token为：" + data);
                    }

                    @Override
                    public void onFail(Object data, int errCode, String msg) {
                        Log.d("TPush", "注册失败，错误码：" + errCode + ",错误信息：" + msg);
                    }
                });
    }

    public void initView() {

        // TODO Auto-generated method stub
        drawerLayout=(DrawerLayout) findViewById(R.id.id_drawerlayout);
        drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        });

       init();
        listView=(ListView) findViewById(R.id.listview);
    //    str = new String[] { "首页", "入库", "领用","盘点","租赁","归还","转移","调拨","报废","报修"};
        Menu_Adapter adapter = new Menu_Adapter(this,
                R.layout.munu_item, linkMains);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        fragmentManager = getSupportFragmentManager();
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fragment5 = new Fragment5();
        fragment6 = new Fragment6();
        fragment7 = new Fragment7();
        Allot = new Allot();
        Scrap = new Scrap();
        Repairs = new Repairs();
        fragmentManager.beginTransaction()
                .replace(R.id.id_framelayout2, fragment1).commit();
        //drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开  不设置则不会打开

    }

    private void init() {
        linkMains.add(new Menu_bean(R.mipmap.main,"首页"));
        linkMains.add(new Menu_bean(R.mipmap.main,"入库"));
        linkMains.add(new Menu_bean(R.mipmap.main,"领用"));
        linkMains.add(new Menu_bean(R.mipmap.main,"盘点"));
        linkMains.add(new Menu_bean(R.mipmap.main,"租赁"));
        linkMains.add(new Menu_bean(R.mipmap.main,"归还"));
        linkMains.add(new Menu_bean(R.mipmap.main,"转移"));
        linkMains.add(new Menu_bean(R.mipmap.main,"调拨"));
        linkMains.add(new Menu_bean(R.mipmap.main,"报废"));
        linkMains.add(new Menu_bean(R.mipmap.main,"报修"));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        switch (position) {
            case 0:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment1).commit();
                break;
            case 1:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment2).commit();
                break;
            case 2:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment3).commit();
                break;
            case 3:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment4).commit();
                break;
            case 4:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,fragment5).commit();
                break;
            case 5:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,fragment6).commit();
                break;
            case 6:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,fragment7).commit();
                break;
            case 7:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,Allot).commit();
                break;
            case 8:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,Scrap).commit();
                break;
            case 9:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2,Repairs).commit();
                break;
        }
      //  setTitle(linkMains.get(linkMains.size()));
        drawerLayout.closeDrawers();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                //将整个虚拟机的内容完全退出，而不是只退出其中一个窗口
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

   // @Override
  //  protected void onSaveInstanceState(Bundle outState) {
 //       super.onSaveInstanceState(outState);
    //}
       //  protected void onSaveInstanceState(Bundle outState) {           //super.onSaveInstanceState(outState);
             // 注释掉该方法， 即不保存状态
          //   }0-

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

